package com.app.mymusic.utils;

import com.app.mymusic.model.Songs;
import com.app.mymusic.service.ApiService;
import com.app.mymusic.test.dagger2.User;

import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016-12-1.
 */

public class RetrofitUtil {

    private ApiService apiService;
    private static RetrofitUtil instance=new RetrofitUtil();
    public RetrofitUtil() {

    }
    public static RetrofitUtil getInstace(){
        return  instance;
    }

    public RetrofitUtil  initRetrofit(String url){
        Retrofit retrofit=new Retrofit.Builder().baseUrl(url)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         apiService=retrofit.create(ApiService.class);
        return  instance;

    }

    public void login(String name, String psw, String id, Subscriber<ResponseBody> subscriber){
        apiService.login(name,psw,id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
    public void getCharts(String type,String offset,Subscriber<Songs> subscriber){
        apiService.getCharts("json","","webapp_music","baidu.ting.billboard.billList",type,"10",offset)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     *
     * @param subscriber
     * @param ip
     */
    public void getData(Subscriber<User> subscriber, String ip) {

        apiService.getUser()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
